import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class TextBoxSong
{
	public int x;
	public int y;
	public int width = 200;
	public String text;
	public String emptyTextBox;
	public ScrollBar scrollBar;
	public boolean active;

	public ArrayList<ButtonSong> suggestionButtonsToShow;


	public float buttonTimer;
	public int heightVal;
	public int suggestionHeight;
	
	SearchSongThread p;



	////////////////////////////////////////////////////////////////////////////////////////////////////
	public TextBoxSong(String emptyTextBox)
	{
		text = "";
		scrollBar = new ScrollBar();
		active=false;
		this.emptyTextBox = emptyTextBox;
		suggestionButtonsToShow = new ArrayList<ButtonSong>();
		updateButtons();
		
		p = new SearchSongThread();
	}

	public void draw()
	{
		// If not in songs mode don't show the textbox
		if(GLOBAL.analysisType != 3) return;

		GLOBAL.processing.rectMode(GLOBAL.processing.CORNER);
		GLOBAL.processing.noFill();
		GLOBAL.processing.stroke(80);
		GLOBAL.processing.strokeWeight(2);
		GLOBAL.processing.rect(x, y, width, 30);

		GLOBAL.processing.fill(GLOBAL.textColor);	
		GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
		GLOBAL.processing.textAlign(GLOBAL.processing.LEFT);

		suggestionHeight = 130;

		if(active){
			if(buttonTimer==(float)0.0){
				scrollBar.val=0;
				buttonTimer = GLOBAL.timer;
				heightVal=0;
			}
			heightVal = (int)GLOBAL.processing.lerp(heightVal,suggestionHeight,(float) 0.3);
			drawSuggestions();
		}
		else{if(buttonTimer!=(float)0.0){heightVal=suggestionHeight;}
		heightVal = (int)GLOBAL.processing.lerp(heightVal,0,(float) 0.3);
		if(heightVal!=0)
			drawSuggestions();
		else 
			GLOBAL.processing.text(emptyTextBox, x + 5, y + 20);

		buttonTimer=(float)0.0;

		}
//		if(p.isAlive()){
//			GLOBAL.angleArtist = GLOBAL.loadingCircle(20,100,GLOBAL.angleArtist);
//		//System.out.println("loading");
//		}

	}

	public void drawSuggestions(){
		GLOBAL.processing.text(text, x + 5, y + 20);
		GLOBAL.processing.fill(44,160);
		GLOBAL.processing.stroke(80);
		GLOBAL.processing.strokeWeight(1);
		GLOBAL.processing.rect(x, y+30, width, heightVal);


		scrollBar.width = 15;
		scrollBar.x =  x +width-15;
		scrollBar.y = y+30;
		scrollBar.height = heightVal-4;
		if(heightVal>scrollBar.y-y && suggestionButtonsToShow.size()>4){
			scrollBar.draw();  
			scrollBar.size = 1/(float)(suggestionButtonsToShow.size()-2);
		}
		showButtons();

	}


	public void keyTyped()
	{
		if(GLOBAL.processing.key==GLOBAL.processing.ENTER){

			if(suggestionButtonsToShow.size()>0){
				Song s =suggestionButtonsToShow.get(0).song;
				GLOBAL.songAddRemove(s);
				this.active=false;
				text = new String();
			}
		}
		else if(GLOBAL.processing.key == 8)
		{
			if(text.length() > 0) text = text.substring(0, text.length() - 1);
		}
		else if(text.length()>22){
			text = text.substring(0, text.length() - 1);
			text += GLOBAL.processing.key;
		}
		else
		{
			text += GLOBAL.processing.key;
		}

		updateButtons();

	}


	public boolean mouseOver()
	{
		if(GLOBAL.processing.mouseX > x && GLOBAL.processing.mouseX < x + width && 
				GLOBAL.processing.mouseY > y && GLOBAL.processing.mouseY < y + 30) return true;
		return false;
	}

	public boolean mouseOverSuggestions(){

		if(GLOBAL.processing.mouseX > x && GLOBAL.processing.mouseX < x + width && 
				GLOBAL.processing.mouseY > y+30 && GLOBAL.processing.mouseY < y + 165)return true;
		return false;
	}

	//show  buttons in the suggestions
	private void showButtons() {

		float j = Main.map (scrollBar.val,0,1,0,suggestionButtonsToShow.size()-3);
		if(j<0)
			j=0;

		for(int i=(int)j;i<=(int)j+5 && i<suggestionButtonsToShow.size()&&i-(int)j<+heightVal/23-0.3;i++){

			ButtonSong b = suggestionButtonsToShow.get(i);
			if(b.state){
				if(suggestionButtonsToShow.size()>i){
					Song s =suggestionButtonsToShow.get(i).song;
					GLOBAL.songAddRemove(s);
					this.active=false;
					text = new String();
				}
				updateButtons();
				break;
			}else{
				b.y=y+30+(i-(int)j)*25;
				b.draw();
			}
		}		
	}


	public void updateButtons(){
		suggestionButtonsToShow = new ArrayList<ButtonSong>();

		if(text.length()<=1)return;

		if(p.isAlive())p.stop();
	    p = new SearchSongThread();
		p.start();

	}

	public void mousePressed() {
		float j = Main.map (scrollBar.val,0,1,0,suggestionButtonsToShow.size()-3);
		if(j<0)
			j=0;
		for(int i=(int)j;i<=(int)j+5 && i<suggestionButtonsToShow.size()&&i-(int)j<+heightVal/23-0.3;i++){
			ButtonSong b = suggestionButtonsToShow.get(i);
			if(b.mouseOver())b.mouseClicked();
		}
	}
	
	
	public class SearchSongThread extends Thread{
		
		public void run() {
			
			Statement stmt;
			ResultSet res ;
			
			try
			{
				String endString = text.substring(0,text.length()-1);
				Character c = text.charAt(text.length()-1);
				c = new Character((char) (c.charValue()+1));

				endString = endString.concat(c.toString());

				String sql = "SELECT DISTINCT S.song_title, A.name " +
						"FROM songs as S INNER JOIN artists as A on S.artist_code == A.artist_code " +
						"WHERE S.song_title >= '"+
						text.replace("'", "''")+"' AND S.song_title < '"+endString.replace("'", "''")+"' LIMIT 50";
				stmt = GLOBAL.user_times_con.createStatement();
				res = stmt.executeQuery(sql);

			}
			catch(SQLException e)
			{
				System.out.println("Error creating or running statement: " + e.toString());
				return;
			}



			try
			{
				String song_name;
				String artist_name;
				while(res.next())
				{
					song_name = res.getString("song_title");
					artist_name = res.getString("name");
					Song s = new Song(song_name, artist_name);
					suggestionButtonsToShow.add(new ButtonSong(song_name+" - "+artist_name, x+5, 0, s));
				}
			}
			catch(Exception e)
			{
				System.out.println("Error processing results: " + e.toString());
				try{
					res.close();
					stmt.close();
				}
				catch(Exception ex){}
				return;
			}
			
		}
		
	}

}